function getDate() {
    var date = new Date()
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var dates = date.getDate();
    var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    var day = date.getDay()
    return year + '年' + month + '月' + dates + '日  ' + arr[day]
}
var el_date = document.querySelector('.date')
el_date.innerHTML = getDate()
var el_h = document.querySelector('.h')
var el_m = document.querySelector('.m')
var el_s = document.querySelector('.s')
// 开启定时器
getTime() //先调用一次，防止第一次刷新页面空白
setInterval(getTime, 1000)
function getTime() {
    var date = new Date();
    var h = date.getHours();
    h = h < 10 ? '0' + h : h;
    el_h.innerHTML = h + ':'
    var m = date.getMinutes();
    m = m < 10 ? '0' + m : m;
    el_m.innerHTML = m + ':'
    var s = date.getSeconds();
    s = s < 10 ? '0' + s : s;
    el_s.innerHTML = s
}
var app = new Vue({
    el: "#app",
    data: {
        servers: ["Angle", "Agnes", "Mrlic"],
        interface: ["/angle/get_status", "/agnes/get_status", "/mrlic/downloading"],
        base_url: "http://kunvm1.ap.health.ge.com:8089",
        builds: "",
        build_rules: [],
        new_rule: "",
        tocken: "",
        angle_idle: true,
        angle_pid: "",
        angle_task: "",
        angle_duration: "",
        agnes_idle: true,
        agnes_pid: "",
        agnes_task: "",
        agnes_duration: "",
        mrlic_idle: true,
        mrlic_pid: "",
        mrlic_downloading_build: "",
        mrlic_duration: "",
    },
    methods: {
        insert_rule: function () {
            let that = this 
            let url = this.base_url + "/mrlic/insert_rule" + "?rule=" + this.new_rule + "&tocken=" + this.tocken
            axios.get(url).then(
                (response) => {
                    if (response.data.insert == "ok") {
                        that.query_rules()
                    }else{
                        alert("请输入rule和tocken")
                    }
                },
                (err) => {}
            )
         },
        query_builds: function (build_path) {
            let url = this.base_url + build_path
            let that = this
            axios.get(url).then(
                (response) => {
                    that.builds = ""
                    for (let item of response.data.builds) {
                        that.builds += (item + "\n")
                    }
                },
                (err) => { }
            )
        },
        query_rules: function () {
            let that = this
            let url = this.base_url + "/mrlic/rules"
            axios.get(url).then(
                (response) => {
                    that.build_rules = []
                    that.build_rules = response.data.rules
                },
                (err) => { }
            )
        },
        query_angle: function () {
            let that = this
            let url = this.base_url + "/angle/get_status"
            axios.get(url).then(
                (response) => {
                    console.log(response.data.status)
                    if (response.data.status == "idle") {
                        that.angle_idle = true
                    } else {
                        that.angle_idle = false
                        that.angle_pid = response.data.pid
                        that.angle_duration = response.data.duration
                        that.angle_task = response.data.task
                    }
                },
                (err) => { }
            )
        },
        query_agnes: function () {
            let that = this
            let url = this.base_url + "/agnes/get_status"
            axios.get(url).then(
                (response) => {
                    console.log(response.data.status)
                    if (response.data.status == "idle") {
                        that.angle_idle = true
                    } else {
                        that.agnes_idle = false
                        that.agnes_pid = response.data.pid
                        that.agnes_duration = response.data.duration
                        that.agnes_task = response.data.task
                    }
                },
                (err) => { }
            )
        },
        query_mrlic: function () {
            let that = this
            let url = this.base_url + "/mrlic/downloading"
            axios.get(url).then(
                (response) => {
                    if(response.data.status == "idle"){
                        that.mrlic_idle = true
                    }else{
                        that.mrlic_idle = false
                        that.mrlic_pid = response.data.pid
                        that.mrlic_duration = response.data.duration
                        that.mrlic_downloading_build = response.data.downloading_build
                    }
                },
                (err) => { }
            )
        },

        init_load: function () {
            this.query_rules()
            this.query_builds("/mrlic/builds")
            this.query_angle()
            this.query_agnes()
            this.query_mrlic()
        }

    },
    watch: {
        title: {
            handler: function () {
                this.init_load()
            },
            immediate: true
        }
    }
})